package com.softer.chengxin.enterprise.controller;

import com.github.pagehelper.PageInfo;
import com.softer.chengxin.common.domin.Common;
import com.softer.chengxin.common.domin.CommonResult;
import com.softer.chengxin.common.domin.Page;
import com.softer.chengxin.common.enums.ResultEnum;
import com.softer.chengxin.enterprise.entity.Enterprise;
import com.softer.chengxin.enterprise.service.EnterpriseService;
import com.softer.chengxin.login.service.impl.LoginServiceimpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * 企业缺失录入
 *
 * @author Gong Zhiyang
 * @date 2019/12/12
 */
@RestController
@RequestMapping("/api/enterprise")
public class EnterpriseController {
    @Autowired
    private EnterpriseService enterpriseService;
    @Autowired
    private LoginServiceimpl loginService;

    /**
     * 企业缺失录入
     *
     * @param enterprise 缺失企业信息
     * @return 新增结果
     */
    @PostMapping("/add")
    public CommonResult add(Enterprise enterprise, HttpSession session) {
        //  登录名
        String loginName = (String) session.getAttribute(Common.LOGIN_NAME);
        CommonResult result = loginService.checkLoginName(loginName);
        if (ResultEnum.FAIL.getCode() == result.getData()) {
            return result;
        }
        return enterpriseService.add(enterprise, loginName);
    }

    /**
     * 删除缺失企业
     *
     * @param id      企业ID
     * @param session session
     * @return 删除结果
     */
    @PostMapping("/delete")
    public CommonResult delete(int id, HttpSession session) {
        //  登录名
        String loginName = (String) session.getAttribute(Common.LOGIN_NAME);
        CommonResult result = loginService.checkLoginName(loginName);
        if (ResultEnum.FAIL.getCode() == result.getData()) {
            return result;
        }
        return enterpriseService.delete(id);
    }

    /**
     * 修改缺失企业
     *
     * @param enterprise 企业信息
     * @param session    session
     * @return 修改结果
     */
    @PostMapping("/update")
    public CommonResult update(Enterprise enterprise, HttpSession session) {
        //  登录名
        String loginName = (String) session.getAttribute(Common.LOGIN_NAME);
        CommonResult result = loginService.checkLoginName(loginName);
        if (ResultEnum.FAIL.getCode() == result.getData()) {
            return result;
        }
        return enterpriseService.update(enterprise, loginName);
    }

    /**
     * 查询缺失企业详情
     *
     * @param id      企业ID
     * @param session session
     * @return 企业信息
     */
    @PostMapping("/getInfo")
    public Enterprise getInfo(int id, HttpSession session) {
        //  登录名
        String loginName = (String) session.getAttribute(Common.LOGIN_NAME);
        CommonResult result = loginService.checkLoginName(loginName);
        if (ResultEnum.FAIL.getCode() == result.getData()) {
            return new Enterprise();
        }
        return enterpriseService.getInfo(id);
    }

    /**
     * 查询缺失企业列表
     *
     * @param page       分页实体
     * @param enterprise 查询实体
     * @param session    session
     * @return 缺失企业列表
     */
    @PostMapping("/getList")
    public PageInfo<Enterprise> getList(Page page, Enterprise enterprise, HttpSession session) {
        //  登录名
        String loginName = (String) session.getAttribute(Common.LOGIN_NAME);
        CommonResult result = loginService.checkLoginName(loginName);
        if (ResultEnum.FAIL.getCode() == result.getData()) {
            return new PageInfo<>();
        }
        return enterpriseService.getList(page, enterprise);
    }


}
